package com.jprocms.module.cms.service.accessstatistics;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;
import com.jprocms.module.cms.controller.admin.accessstatistics.vo.AccessStatisticsListReqVO;
import com.jprocms.module.cms.dal.dataobject.accessstatistics.AccessStatisticsDO;
import com.jprocms.module.cms.dal.mysql.accessstatistics.AccessStatisticsMapper;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static com.jprocms.module.cms.dal.dataobject.accessstatistics.AccessStatisticsDO.DAY_DISPLAY_FORMATTER;
import static com.jprocms.module.cms.dal.dataobject.accessstatistics.AccessStatisticsDO.DAY_FORMATTER;

/**
 * 访问统计 Service 实现类
 *
 * @author jprocms
 */
@Service
@Validated
public class AccessStatisticsServiceImpl extends ServiceImpl<AccessStatisticsMapper, AccessStatisticsDO> implements AccessStatisticsService {

    @Resource
    private AccessStatisticsMapper accessStatisticsMapper;

    @Override
    public List<AccessStatisticsDO> getAccessStatisticsList(AccessStatisticsListReqVO exportReqVO) {
        return accessStatisticsMapper.selectList(exportReqVO);
    }

    /**
     * 按日期统计
     *
     * @param siteId 站点ID
     * @param type   类型
     * @param begin  开始日期
     * @param end    介绍日期
     * @return 统计结果
     */
    public List<AccessStatisticsDO> statByDate(Long siteId, Integer type, @Nullable String begin, @Nullable String end) {
        return PageMethod.offsetPage(0, AccessStatisticsDO.DISPLAY_MAX_SIZE, false)
                .doSelectPage(() -> accessStatisticsMapper.statByDate(siteId, type, begin, end));
    }

    public Page<AccessStatisticsDO> statByDate(Long siteId, Integer type, @Nullable String begin, @Nullable String end,
                                               int page, int pageSize) {
        return PageMethod.startPage(page, pageSize).doSelectPage(() -> accessStatisticsMapper.statByDate(siteId, type, begin, end));
    }

    public List<AccessStatisticsDO> selectFullList(@Nullable Long siteId, @Nullable Integer type, String begin, String end) {
        AccessStatisticsListReqVO reqVO = new AccessStatisticsListReqVO();
        reqVO.setSiteId(siteId);
        reqVO.setStatType(type);
        reqVO.setDateBeginString(begin);
        reqVO.setDateEndString(end);
        return AccessStatisticsDO.fillEmptyDate(getAccessStatisticsList(reqVO), begin, end, DAY_FORMATTER, DAY_DISPLAY_FORMATTER,
                date -> date.plusDays(1), AccessStatisticsDO::new);
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteBeforeDateString(String dataString) {
        accessStatisticsMapper.deleteBeforeDateString(dataString);
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteByDateString(String dateString, @Nullable Integer type) {
        accessStatisticsMapper.deleteByDateString(dateString, type);
    }

    //TODO  站点删除统计数据需要删除


}
